'''
	Consider the case of vandalism and output the revisions that are free from revisions.
	The revisions corresponding to the new users and total revisions are output in different files.
'''	


import database as db
import config as cg

	
def getSizeOfText(rev):
	conn = db.connectDb()
	Text = db.extractTextForRev(conn, rev);
	try:
		Text = Text[0].split(' ');
		num = len(Text);
		db.closeConn(conn);
		return num
	except:
		return -1;
	
def getListOfSizeOfText():
	print "List of size "
	conn = db.connectDb();
	revList = db.extractRevOrdered(conn);
	sizeOld = getSizeOfText(revList[0][0])
	i = 0;
	sizeFile = open(cg.size, 'w');
	for item in revList:
		#print "Entered yet?  "
		sizeNew = getSizeOfText(item[0]);
		if( sizeNew == -1 ):
			#print "Some error in text extraction " + item[0]
			continue;

		if abs( sizeNew - sizeOld ) < abs(0.1*sizeOld) and sizeNew is not 1:
			i += 1;
			sizeFile.write(str(i) + " " + item[0] + " " + str(sizeNew) + '\n');
			print i, item[0], sizeNew;
		sizeOld = sizeNew;
	#print revList;
	db.closeConn(conn);

def getListOfSizeOfText1():
	print "List of size1 "
	conn = db.connectDb();
	revList = db.extractDataTbRev(conn);
	sizeOld = getSizeOfText(revList[0][0])
	i = 0;
	sizeFile = open(cg.size1, 'w');
	for item in revList:
		sizeNew = getSizeOfText(item[0]);

		if( sizeNew == -1 ):
			print "Some error in text extraction " + item[0]
			continue;

		if abs( sizeNew - sizeOld ) < abs(0.1*sizeOld) and sizeNew is not 1:
			i += 1;
			sizeFile.write(str(i) + " " + item[0] + " " + str(sizeNew) + '\n');
			print i, item[0], sizeNew;
		sizeOld = sizeNew;
	#print revList;
	db.closeConn(conn);	

#print "In the file "

getListOfSizeOfText()
#getListOfSizeOfText1()
